package books.to.africa


class DonorRegisterController {

	def NotifierService
	
//	def scaffold = true
	def index = {redirect (action:donorHome)}
	//def home = {render (view: "donorRegister.gsp")}
	
	def donorHome = {render (view: "donorRegister.gsp")}
	
	
	def donateBook = {
			
		}
	
	def register = {
		def donorrole = Role.findByAuthority('ROLE_DONOR') 
		//code for registering without photo...
		def newuser = User.findByUsername(params.userName) ?: 
			new DonorRegister (
				username: params.userName,
				password: params.password,
				enabled: true,
				email: params.email,
				dl: null
				
				).save(failOnError:true)
				
			UserRole.create(newuser, donorrole)
			try{
				NotifierService.contactUser(user.username, user.email)
				render (view: "/donorRegister/donorRegisterSuccess.gsp")
			}
			catch(all){
				render (view: "/donorRegister/donorRegisterNoEmail.gsp")
			}
	}
		
	def addBook = {
		
		def donor = DonorRegister.findByUserName('donor1')
		def b = new Book(params)
		b.save()
		donor.addToBooks(b)
		donor.save()
		redirect (action: donateBook)
	}

}
